﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Tester.DL645.Core;

namespace Tester.DL645.Instructions
{
    /// <summary>
    /// 指令编译者
    /// </summary>
    public class InstructionBuilder
    {
        [Required]
        public string Address { get; set; }
        [Required]
        public string Password { get; set; }
        [Required]
        /// <summary>
        /// 
        /// </summary>
        public string Auth { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="address"></param>
        /// <param name="password"></param>
        /// <param name="auth"></param>
        public InstructionBuilder(string address, string password = "0000", string auth = "02")
        {
            this.Address = address;
            this.Password = password;
            this.Auth = auth;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="instructionType"></param>
        /// <returns></returns>
        public AbstractInstruction CreateInstruction(Core.InstructionType instructionType)
        {
            switch (instructionType)
            {
                case Core.InstructionType.Address:
                    return this.CreateAddressInstruction().Build();
                case Core.InstructionType.On:
                    return this.CreateOnInstruction(this.Address, this.Password, this.Auth).Build();
                case Core.InstructionType.Off:
                    return this.CreateOffInstruction(this.Address, this.Password, this.Auth);
                case Core.InstructionType.Energy:
                    return this.CreateEnergyInstruction(this.Address).Build();
                case Core.InstructionType.APhaseVoltage:
                    return this.CreatePhaseVoltageInstruction(this.Address, Core.Phase.A).Build();
                case Core.InstructionType.BPhaseVoltage:
                    return this.CreatePhaseVoltageInstruction(this.Address, Core.Phase.B).Build();
                case Core.InstructionType.CPhaseVoltage:
                    return this.CreatePhaseVoltageInstruction(this.Address, Core.Phase.C).Build();
                case Core.InstructionType.APhaseCurrent:
                    return this.CreatePhaseCurrentInstruction(this.Address, Core.Phase.A).Build();
                case Core.InstructionType.BPhaseCurrent:
                    return this.CreatePhaseCurrentInstruction(this.Address, Core.Phase.B).Build();
                case Core.InstructionType.CPhaseCurrent:
                    return this.CreatePhaseCurrentInstruction(this.Address, Core.Phase.C).Build();
                default:
                    return new NoInstruction().Build();
            }

        }
        /// <summary>
        /// 读地址
        /// </summary>
        /// <returns></returns>
        public virtual AbstractInstruction CreateAddressInstruction()
        {
            return new AddressInstruction().Build();
        }

        /// <summary>
        /// 合闸
        /// </summary>
        /// <param name="address"></param>
        /// <param name="password"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public AbstractInstruction CreateOnInstruction(string address,string password,string auth)
        {
            return new OnInstruction(address, password, auth).Build();
        }

        /// <summary>
        /// 分闸
        /// </summary>
        /// <param name="address"></param>
        /// <param name="password"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public virtual AbstractInstruction CreateOffInstruction(string address, string password, string auth)
        {
            return new OffInstruction(address, password, auth).Build();
        }
         
        /// <summary>
        /// 电量
        /// </summary>
        /// <param name="instructionType"></param>
        /// <returns></returns>
        public AbstractInstruction CreateEnergyInstruction(string address)
        {
            return new EnergyInstruction(address);
        }
        /// <summary>
        /// 相位电压
        /// </summary>
        /// <param name="address"></param>
        /// <param name="phase"></param>
        /// <returns></returns>
        public AbstractInstruction CreatePhaseVoltageInstruction(string address,Phase phase)
        {
            return new PhaseVoltageInstruction(address, phase);
        }
        /// <summary>
        /// 相位电流
        /// </summary>
        /// <param name="address"></param>
        /// <param name="phase"></param>
        /// <returns></returns>
        public AbstractInstruction CreatePhaseCurrentInstruction(string address, Phase phase)
        {
            return new PhaseCurrentInstruction(address, phase);
        }


    }
}
